from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.prod_category_fields_def import brand_list_fields,brand_fields
from services.online_mall.prod_brand_service import ProdBrandService

class ProdBrandsApi(Resource):
    @login_required
    @marshal_with(brand_list_fields)
    @check_perms(perms=["prod:brand:list"])
    def get(self):
        """
        获取产品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=False,location='args')
        parser.add_argument("brief",type=str,required=False,location='args')
        parser.add_argument("content",type=str,required=False,location='args')
        parser.add_argument("first_char",type=str,required=False,location='args')
        parser.add_argument("page",type=int,required=False,default=-1,location='args')
        parser.add_argument("page_size",type=int,required=False,default=10,location='args')
        args = parser.parse_args()
        return ProdBrandService.get_brand_list(current_user,args)
    
    @login_required
    @marshal_with(brand_fields)
    @check_perms(perms=["prod:brand:create"])
    def post(self):
        """
        获取产品分类列表
        """
        parser = reqparse.RequestParser()
        # parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=True,location='args')
        parser.add_argument("pic_file_id",type=str,required=False,location='args')
        parser.add_argument("memo",type=str,required=False,location='args')
        parser.add_argument("brief",type=str,required=False,location='args')
        parser.add_argument("content",type=str,required=False,location='args')
        parser.add_argument("first_char",type=str,required=False,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        args = parser.parse_args()
        return ProdBrandService.create_brand(current_user,args)

    @login_required
    @marshal_with(brand_fields)
    @check_perms(perms=["prod:brand:update"])
    def put(self):
        """
        获取产品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("name",type=str,required=True,location='args')
        parser.add_argument("pic_file_id",type=str,required=False,location='args')
        parser.add_argument("memo",type=str,required=False,location='args')
        parser.add_argument("brief",type=str,required=False,location='args')
        parser.add_argument("content",type=str,required=False,location='args')
        parser.add_argument("first_char",type=str,required=False,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        parser.add_argument("seq",type=int,required=False,location='args')
        args = parser.parse_args()
        return ProdBrandService.update_brand(current_user,args)
    
    @login_required
    @marshal_with(brand_fields)
    @check_perms(perms=["prod:brand:del"])
    def delete(self):
        """
        获取产品分类列表
        """
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        args = parser.parse_args()
        brand_id = args.get("id")
        return ProdBrandService.delete_brand(current_user,brand_id)
    
api.add_resource(ProdBrandsApi, '/brands')